//
//  MansoryScrollViewAutoViewController.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/6/15.

@interface MasonryScrollView : UIScrollView
@end

@implementation MasonryScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}
#pragma mark Initialize View Style
-(void)setUp
{
    self.backgroundColor = [UIColor whiteColor];
    self.pagingEnabled = YES;
//    [self horizontalScroll];
    [self verticalScrollView];
}

#pragma mark View Algorithm Processor
#pragma mark Setter Method View Style
-(void)horizontalScroll
{
    UIView * contrainerView = [UIView new];
    contrainerView.backgroundColor = [UIColor whiteColor];
    contrainerView.layer.masksToBounds = YES;
    contrainerView.layer.cornerRadius = 2.f;
    [self addSubview:contrainerView];
    //
    [contrainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        UIEdgeInsets  insets = UIEdgeInsetsZero;
        make.edges.mas_equalTo(self).with.insets(insets);
        make.height.mas_equalTo(self.mas_height).with.offset(0.f);
    }];
    __block UIView * lastView = nil;
    for (int i = 0; i < 10; i++) {
        UIView * subView = [UIView new];
        subView.layer.masksToBounds = YES;
        subView.layer.cornerRadius = 2.f;
        subView.backgroundColor = [UIColor colorWithRed:arc4random()%255 /256.0 green:arc4random()%255 /256.f blue:arc4random()%255 /256.0 alpha:1.f];
        [contrainerView addSubview:subView];
        [subView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.mas_equalTo(contrainerView);
            make.width.mas_equalTo(self.mas_width).with.multipliedBy(1.f);
            if (lastView) {
                make.left.mas_equalTo(lastView.mas_right).offset(5.f);
            }
            else
            {
                make.left.mas_equalTo(contrainerView.mas_left).with.offset(5.f);
            }
        }];
        lastView = subView;
    }
    [contrainerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(lastView.mas_right).with.offset(-10);
    }];
}

-(void)verticalScrollView
{
    UIView * contrainterView = [UIView new];
    contrainterView.backgroundColor = [UIColor whiteColor];
    contrainterView.layer.masksToBounds = YES;
    contrainterView.layer.cornerRadius  = 2.f;
    [self addSubview:contrainterView];
    
    [contrainterView mas_makeConstraints:^(MASConstraintMaker *make) {
        UIEdgeInsets insets =  UIEdgeInsetsZero;
        make.edges.mas_equalTo(self).with.insets(insets);
        make.width.mas_equalTo(self.mas_width).with.offset(0);
    }];
    
    __block  UIView * lastView = nil;
    for (int i =  0 ; i < 10; i++) {
        UIView * subView = [UIView new];
        subView.backgroundColor = [UIColor colorWithRed:arc4random()%255 / 256.0 green:arc4random()%255 / 256.0 blue:arc4random()%255 / 256.0 alpha:1.f];
        subView.layer.masksToBounds = YES;
        subView.layer.cornerRadius = 2.f;
        [contrainterView addSubview:subView];
         [subView mas_makeConstraints:^(MASConstraintMaker *make) {
             make.left.right.mas_equalTo(contrainterView);
             make.height.mas_equalTo(self.mas_height).with.multipliedBy(1.f);
             if (lastView) {
                 
                 make.top.mas_equalTo(lastView.mas_bottom).with.offset(8);
             }
             else
             {
                 make.top.mas_equalTo(contrainterView.mas_top).with.offset(8);
             }
         }];
        lastView = subView;
    };
    [contrainterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(lastView.mas_bottom).with.offset(-8.f);
    }];
}
#pragma mark Getter Method View Style
#pragma mark IBOutlet Action View
#pragma mark Possess  Delegate View（委托代理者）
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

}
#pragma mark Other Method
-(void)layoutSubviews
{
    [super layoutSubviews];
}
@end
#import "MansoryScrollViewAutoViewController.h"
@interface MansoryScrollViewAutoViewController()

@property (strong, nonatomic) MasonryScrollView * masonryScrollView;
@end

@implementation MansoryScrollViewAutoViewController

#pragma mark ViewController LifeCycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setSubObjectView];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}
#pragma mark ViewController AsynRequestData
-(void)accountInfoAsynRequest;
{
    
}
#pragma mark ViewController Algorithm Processor
- (void) reverseSourceData:(id)parameter;
{

}
#pragma mark ViewController Initialize SubObjectView
-(void)setSubObjectView
{
    self.view.backgroundColor = KColor(247, 247, 247);
    [self.view addSubview:self.masonryScrollView];
    [self.masonryScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        UIEdgeInsets insets = UIEdgeInsetsMake( kNavigationBarHeight+ 10,10, 10, 10);
        make.edges.mas_equalTo(self.view).with.insets(insets);
    }];
}
#pragma mark --viewController getter methods
-(MasonryScrollView *)masonryScrollView
{
    _masonryScrollView = ({
        if (!_masonryScrollView) {
            _masonryScrollView =  [[MasonryScrollView alloc]init];
        }
        _masonryScrollView;
    });
    return _masonryScrollView;
}
#pragma mark Possess  Delegate ViewController（委托代理者）
#pragma mark IBOutlet Action ViewController

#pragma mark Other Method
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
}
@end
